<?php

!defined('IN_APP') && exit('Access Denied');

class usermodel extends modelbase{

    function get_by_uid($uid) {
    	$user = $this->collection->findOne(array("uid" => $uid));
    	//$user['lastlogin'] = tdate($user['lastlogin']);
        return $user;
    }

    function get_by_username($username) {
        $user = $this->collection->findOne(array("username" => $username));
        return $user;
    }

    function get_by_email($email) {
        $user = $this->collection->findOne(array("email" => $email));
        return $user;
    }

    function get_by_phone($phone) {
        $user = $this->collection->findOne(array("phone" => $phone));
        return $user;
    }
    
    function export_csv($filepath, $query, $fields ) {
        $fp = fopen($filepath,'w');
        $cursor = $this->collection->find($query, $fields)->sort(array('uid'=>1));; 
        foreach ($cursor as $user) {
            $user['format_lastlogin'] = tdate($user['lastlogin']);
            $user['format_regtime'] = tdate($user['regtime']);
            $user['format_bday'] = tdate($user['bday']);
            fputcsv($fp,$user);
        }
        fclose($fp);
        $text=file_get_contents($filepath);
        $text=iconv("UTF-8", "GBK", $text);
        file_put_contents($filepath,$text);
    }
    
    function get_list($criteria=array() , $start=0, $limit=10) {
        $userlist = array();
        $cursor = $this->collection->find($criteria)->skip($start)->limit($limit)->sort(array('uid'=>1));
        foreach ($cursor as $user) {
            $user['format_lastlogin'] = tdate($user['lastlogin']);
            $user['format_regtime'] = tdate($user['regtime']);
            $user['format_bday'] = tdate($user['bday']);
            $userlist[]= $user;
        }
        return $userlist;
    }

    function refresh($user) {
        @$sid = tcookie('sid');
        $this->base->user = $user;
        //$this->db->query("UPDATE " . DB_TABLEPRE . "user SET `lastlogin`={$this->base->time}  WHERE `uid`=$uid"); //更新最后登录时间
        //$this->db->query("REPLACE INTO " . DB_TABLEPRE . "session (sid,uid,islogin,`time`) VALUES ('$sid',$uid,$islogin,{$this->base->time})");
        $uid = $user['uid'];
        $password = $user['password'];
        $auth = strcode("$uid\t$password", $this->base->setting['auth_key'], 'ENCODE');
		tcookie('auth', $auth, 864300*30);
    }

   
    /* 添加用户，本函数需要返回uid */
    function add($user) {
		$uid=$this->get_autoincre_id('user');
		$user['uid'] = $uid;
		$user['regip'] = $this->base->ip;
		$user['regtime'] = $this->base->time;
		$user['password'] = md5($user['password']);
		$user['groupid'] = 3;	
		$this->collection->insert($user); 
		return $uid;
    }

    /* 修改用户密码 */

    function uppass($uid, $password) {
        $this->collection->update(array("uid" => $uid),array('$set' => array("password" => md5($password)))); 
    }

  	/* 修改个人资料 */
    function update($uid, $user) {
        $this->collection->update(array("uid" => $uid),array('$set' => $user) ); 
    }


    /* 删除用户 */
    function remove($uids) {
		$criteria=array(
			'uid'=> array('$in'=>$uids)
		);
		$this->collection->remove($criteria);
    }

    function logout() {
        $sid = $this->base->user['sid'];
        tcookie('sid', '', 0);
        tcookie('auth', '', 0);
        tcookie('loginuser', '', 0);
        if ($sid) {
            //$this->db->query('DELETE FROM ' . DB_TABLEPRE . 'session WHERE sid=\'' . $sid . '\'');
        }
    }

    function save_code($code) {
        $uid = $this->base->user['uid'];
        $sid = $this->base->user['sid'];
        $islogin = $this->db->result_first("SELECT islogin FROM " . DB_TABLEPRE . "session WHERE sid='$sid'");
        $islogin = $islogin ? $islogin : 0;
        $this->db->query("REPLACE INTO " . DB_TABLEPRE . "session (sid,uid,code,islogin,`time`) VALUES ('$sid',$uid,'$code','$islogin',{$this->base->time})");
    }

    function get_code() {
        $sid = $this->base->user['sid'];
        return $this->db->result_first("SELECT code FROM " . DB_TABLEPRE . "session WHERE sid='$sid'");
    }

 
  /*检测用户名合法性*/
    function check_usernamecensor($username) {
        $censorusername = $this->base->setting['censor_username'];
        $censorexp = '/^('.str_replace(array('\\*', "\r\n", ' '), array('.*', '|', ''), preg_quote(($censorusername = trim($censorusername)), '/')).')$/i';
        if($censorusername && preg_match($censorexp, $username)) {
            return FALSE;
        } else {
            return TRUE;
        }
    }

    /*检测邮件地址合法性*/
    function check_emailaccess($email) {
        $setting = $this->base->setting;
        $accessemail = $setting['access_email'];
        $censoremail = $setting['censor_email'];
        $accessexp = '/('.str_replace("\r\n", '|', preg_quote(trim($accessemail), '/')).')$/i';
        $censorexp = '/('.str_replace("\r\n", '|', preg_quote(trim($censoremail), '/')).')$/i';
        if($accessemail || $censoremail) {
            if(($accessemail && !preg_match($accessexp, $email)) || ($censoremail && preg_match($censorexp, $email))) {
                return FALSE;
            } else {
                return TRUE;
            }
        } else {
            return TRUE;
        }
    }

}

?>